package com.abvnet.project.water.api.web;

import com.abvnet.common.utils.StringUtils;
import com.abvnet.common.utils.file.FileUploadUtils;
import com.abvnet.framework.config.AbvnetConfig;
import com.abvnet.framework.web.controller.BaseController;
import com.abvnet.framework.web.domain.AjaxResult;
import com.abvnet.project.water.project.domain.ProjectTech;
import com.abvnet.project.water.project.service.IProjectTechService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/api/project/tech")
@Slf4j
public class ApiProjectTechController extends BaseController {

    @Autowired
    private IProjectTechService projectTechService;

    @GetMapping("/get")
    public AjaxResult get(@RequestParam("projectId") Long projectId) {
        return AjaxResult.success(projectTechService.selectProjectTechByProjectId(projectId));
    }

    @PostMapping("/edit")
    public AjaxResult editSave(ProjectTech projectTech, @RequestParam(name="refluxFile",required = false) MultipartFile refluxFile,
                               @RequestParam(name="streamFile",required = false) MultipartFile streamFile,
                               @RequestParam(name="aerationFile",required = false) MultipartFile aerationFile,
                               @RequestParam(name="diveFile",required = false) MultipartFile diveFile,
                               @RequestParam(name="otherFile",required = false) MultipartFile otherFile) {
        try {
            multiFile(projectTech, refluxFile, streamFile,aerationFile,diveFile,otherFile);
            return toAjax(projectTechService.updateProjectTech(projectTech));
        } catch (Exception e) {
            log.error("更新工艺运行情况失败！", e);
            return error(e.getMessage());
        }
    }

    private void multiFile(ProjectTech projectTech, MultipartFile refluxFile,
                           MultipartFile streamFile,
                           MultipartFile aerationFile,
                           MultipartFile diveFile,
                           MultipartFile otherFile) throws IOException {
        if(StringUtils.isNotEmpty(projectTech.getRefluxPicDelete())) {
            if(StringUtils.equals("1",projectTech.getRefluxPicDelete())) {
                projectTech.setRefluxPicUrl(StringUtils.EMPTY);
            }
        }
        if (refluxFile != null && !refluxFile.isEmpty()) {
            String refluxUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), refluxFile);
            projectTech.setRefluxPicUrl(refluxUrl);
        }
        if(StringUtils.isNotEmpty(projectTech.getStreamPicDelete())) {
            if(StringUtils.equals("1",projectTech.getStreamPicDelete())) {
                projectTech.setStreamPicUrl(StringUtils.EMPTY);
            }
        }
        if (streamFile != null && !streamFile.isEmpty()) {
            String streamUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), streamFile);
            projectTech.setStreamPicUrl(streamUrl);
        }
        if(StringUtils.isNotEmpty(projectTech.getAerationPicDelete())) {
            if(StringUtils.equals("1",projectTech.getAerationPicDelete())) {
                projectTech.setAerationPicUrl(StringUtils.EMPTY);
            }
        }
        if (aerationFile != null && !aerationFile.isEmpty()) {
            String aerationUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), aerationFile);
            projectTech.setAerationPicUrl(aerationUrl);
        }
        if(StringUtils.isNotEmpty(projectTech.getDivePicDelete())) {
            if(StringUtils.equals("1",projectTech.getDivePicDelete())) {
                projectTech.setDivePicUrl(StringUtils.EMPTY);
            }
        }
        if (diveFile != null && !diveFile.isEmpty()) {
            String diveUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), diveFile);
            projectTech.setDivePicUrl(diveUrl);
        }
        if(StringUtils.isNotEmpty(projectTech.getOtherPicDelete())) {
            if(StringUtils.equals("1",projectTech.getOtherPicDelete())) {
                projectTech.setOtherPicUrl(StringUtils.EMPTY);
            }
        }
        if (otherFile != null && !otherFile.isEmpty()) {
            String otherUrl = FileUploadUtils.upload(AbvnetConfig.getTechPath(), otherFile);
            projectTech.setOtherPicUrl(otherUrl);
        }
    }
}
